package model

import (
	"My_Blog02/utils/errmsg"
	"golang.org/x/crypto/scrypt"
)

func JudgeAccount(account string, password string) int {
	var user User
	err := db.Where("account= ?", account).First(&user).Error
	if err != nil {
		return errmsg.ERROR_NOT_FOUND
	}
	//从数据库存储的密码中提取盐值和原始哈希值
	salt, storedHash, err := ExeractSaltAndHash(user.Password)
	if err != nil {
		return errmsg.ERROR
	}
	//用提取的盐值重新加密输入的密码
	inputHash, err := scrypt.Key([]byte(password), salt, 16384, 8, 1, KeyLen)
	if string(inputHash) != string(storedHash) {
		return errmsg.ERROR
	}
	return errmsg.SUCCSE
}
